<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .upload {
            height: 100px;
            line-height: 100px;
            text-align: center;
            width: 100px;
            border: 1px solid #ddd;
            position: relative;
        }
        
        .plus {
            font-size: 28px;
        }
        
        input {
            top: 0;
            left: 0;
            position: absolute;
            width: 100px;
            height: 100px;
            opacity: 0;
        }
    </style>
</head>

<body>
    <div class="upload">
        <div class="plus">+</div>
        <input type="file" id="ipt" accept=".jpg, .jpeg, .png">
    </div>
    <img src="" id="img" alt="">
</body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    document.getElementById('ipt').addEventListener("change", function(e) {
        console.log(e.target.files); //伪数组
        //转为数组 得到表单格式数据
        Array.from(e.target.files).map((f => {
            let formData = new FormData();
            formData.append("file", f)
            axios.post("http://linweiqin.cn:8001/v1/addimg/avatar", formData).then(res => {
                console.log(res);
                // 拼接图片在线地址
                let imageUrl = "http://linweiqin.cn:8001/img/" + res.data.image_path;
                document.getElementById("img").setAttribute("src", imageUrl);
            })
        }))
    })
</script>

</html>